package com.crm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.dto.RoleDto;
import com.crm.dto.RoleExportDto;
import com.crm.entity.Course;
import com.crm.entity.Role;
import com.crm.mapper.CourseMapper;
import com.crm.mapper.RoleMapper;
import com.crm.service.RoleService;
import com.crm.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RoleServiceImpl  extends ServiceImpl<RoleMapper, Role> implements RoleService  {
@Autowired
private RoleMapper  roleMapper;
    @Override
    public void add(RoleDto roleDto) {
        Role role = new Role();
        role.setRoleName(roleDto.getRoleName());
        role.setRoleKey(roleDto.getRoleKey());
        role.setStatus(roleDto.getStatus());
        roleMapper.insert(role);
        log.info("添加角色成功");
    }
    @Override
    public Page<Role> getPageList(RoleDto roleDto) {
        Page<Role> page = new Page<>(roleDto.getPageNum(),roleDto.getPageSize());
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleDto.getRoleName())) {
            wrapper.like(Role::getRoleName, roleDto.getRoleName()); // 使用 Role 实体的 getName 方法进行模糊匹配
        }
        if (!StringUtils.isEmpty(roleDto.getRoleKey())) {
            wrapper.like(Role::getRoleKey, roleDto.getRoleKey());
        }
        if (roleDto.getStatus() != null){
            wrapper.eq(Role::getStatus, roleDto.getStatus());
        }
//        //todo 时间范围查询
//        Object beginTime = roleDto.getParams().get("beginCreateTime");
//        Object endTime = roleDto.getParams().get("endCreateTime");
//
//        if (beginTime != null && endTime != null &&
//                !beginTime.toString().trim().isEmpty() &&
//                !endTime.toString().trim().isEmpty()){
//            wrapper.between(Role::get, beginTime, endTime);
//        }

        return roleMapper.selectPage(page, wrapper);
    }


    //删除
    @Override
    public boolean batchDeleteRole(Long[] ids) {
        if (ids == null ) {
            throw new IllegalArgumentException("删除的ID列表不能为空");
        }
        //删除
//        int result = roleMapper.deleteBatchIds(Arrays.asList(ids));

//        boolean result = super.removeByIds(ids);
        log.info("批量删除课程完成，共删除 {} 条记录", ids.length);
        return true;
    }

    //修改
    @Override
    public void updateRole(Role role) {
        roleMapper.updateById(role);
        log.info("修改角色成功");
    }

    @Override
    public Role getById(Long roleId) {
        return roleMapper.selectById(roleId);
//        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Role::getRoleId,id);
//        return roleMapper.selectOne(wrapper);
    }

    @Override
    public List<RoleExportDto> exportRoles() {

        List<Role> roles = roleMapper.selectList(null); // 查询所有角色
        return null;
//        return roles.stream()
//                .map(role -> new RoleExportDto(
//                        role.getroleId(),
//                        role.getRoleName(),
//                        role.getRoleKey(),
//                        role.getStatus()
//                ))
//                .collect(Collectors.toList());
    }



}
